/**
 * 转换YAML格式到JSON格式
 */
const process = require('process');
const fs = require('fs');
const yaml = require('js-yaml');
const chalk = require('chalk');

var file = process.argv[2];
var src = yaml.safeLoad(fs.readFileSync(file, 'utf8'));

console.log(chalk.green.bold('Converting ' + src.name));

var out = {
    name: src.name,
    speed: src.speed,
    tracks: []
};

src.tracks.forEach((item) => {
    var notes = [];
    item.notes.forEach((note) => {
        var s = note.toString().split(' ');
        if (s.length > 1) s[1] = Number.parseFloat(s[1]);
        notes.push(s)
    });
    out.tracks.push({
        step: item.step,
        notes: notes
    });
});

fs.writeFileSync(src.name + '.json', JSON.stringify(out));
console.log(chalk.green.bold('Successful converted ' + src.name));
